package org.springblade.common.utils.mq;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.PropertyValueConst;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.thierrysquirrel.annotation.MessageListener;
import com.github.thierrysquirrel.annotation.RocketListener;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.MaintenanceSmsConstant;
import org.springblade.common.constant.TemplateMsgConstant;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.MaintainPlanContentUser;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.IMaintainPlanContentUserService;
import org.springblade.modules.repair.service.WeChatDialogueService;
import org.springblade.modules.repair.vo.ElevatorSimpleInfoVO;
import org.springblade.modules.resource.utils.SmsUtil;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.SimpleUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jackey
 * @date 2023年04月25日 15:42
 */
@Slf4j
@Component
@RocketListener(groupID = "GID_normal",messageModel = PropertyValueConst.BROADCASTING)
public class NormalReceive {

	@Autowired
	private WeChatDialogueService weChatDialogueService;

	@Autowired
	private IMaintainPlanContentService planContentService;

	@Autowired
	private IMaintainPlanContentUserService planContentUserService;

	@Autowired
	private IUserService userService;

	@MessageListener(topic = "normal", tag = "finish_date")
	public void receivePlanFinishDate(String message) {
		log.info("===========接收工单预计完成时间提醒消息，内容:[{}]===========", message);
		JSONObject jsonObject = JSONObject.parseObject(message);
		Long planId = jsonObject.getLongValue("planId");
		Date finishDate = jsonObject.getDate("finishDate");
		MaintainPlanContent planContent = planContentService.getById(planId);
		if (planContent == null) {
			return;
		}
		if(planContent.getStatus().equals(TaskStatusEnum.BY_PROCESSED.getStatus())){
			ElevatorSimpleInfoVO elevatorSimpleInfoVO = planContentService.getElevatorSimpleInfo(planId);
			long a = finishDate.getTime();
			long b = planContent.getFinishDate().getTime();
			if(a == b){
				List<MaintainPlanContentUser> planContentUsers = planContentUserService.list(Wrappers.<MaintainPlanContentUser>lambdaQuery().eq(MaintainPlanContentUser::getPlanId, planId));
				planContentUsers.forEach(e -> {
					if(e.getSignUser().getIsTakePartIn()){

						SimpleUserVO userVO = userService.getSimpleUserInfo(e.getUserId());
						if(ObjectUtil.isNotEmpty(userVO)){
							// 微信公众号消息
							if(StringUtil.isNotBlank(userVO.getOpenid())){

								// 跳转链接
								String skipUrl = String.format("subpages/repair/repariDetail?elevatorId=%s&planId=%s", elevatorSimpleInfoVO.getElevatorId(), planId);
								HashMap<String, String> templateMap = new HashMap<>(5);
								templateMap.put("first", elevatorSimpleInfoVO.getBuildingName() + " " + elevatorSimpleInfoVO.getElevatorAddress());
								templateMap.put("keyword1", String.valueOf(planId));
								templateMap.put("keyword2", "电梯维保");
								templateMap.put("keyword3", MaintainPlanContentEnum.getName(planContent.getTypeStatus()));
								templateMap.put("keyword4", DateUtil.format(planContent.getFinishDate(),"yyyy-MM-dd HH:mm"));
								templateMap.put("keyword5", "是");
								templateMap.put("remark", "请尽快处理！");
								if(planContent.getTypeStatus().equals(MaintainPlanContentEnum.MAINTAIN_WX.getId())){
									templateMap.put("params",skipUrl);
								}

								log.info("===========接收工单预计完成时间提醒公众号消息，内容:[{}]===========", templateMap);
								weChatDialogueService.sendMsg(userVO.getOpenid(), TemplateMsgConstant.T_PLAN_FINISH_DATE,templateMap);
							}
							// 短信消息
//							Map<String, String> smsParam = new HashMap<>(2);
//							smsParam.put("maintainName",userVO.getUserName());
//							smsParam.put("buildingName",elevatorSimpleInfoVO.getBuildingName());
//							smsParam.put("elevatorName",elevatorSimpleInfoVO.getElevatorAddress());
//							smsParam.put("maintainTime",DateUtil.format(planContent.getPlanDate(),"MM/dd"));
//							smsParam.put("maintainType",MaintainPlanContentEnum.getName(planContent.getTypeStatus()));
//							log.info("===========接收工单预计完成时间提醒短信消息，内容:[{}]===========", smsParam);
//							SmsUtil.sendMessage(MaintenanceSmsConstant.MAINTAIN_OVERDUE_NOTICE, BladeConstant.ADMIN_TENANT_ID, smsParam, userVO.getPhone());
						}
					}
				});
			}
		}
	}
}
